package com.sl.hx.shop.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 限时抢购工具类
 */

public class FlashSaleUtil {
    private static String  Zero,Ten,Twelve, Fourteen,Eighteen,Twenty_two; //第一天时间
    private static String tomorrow;//第二天十点
    private static long longZero,longTen,longTwelve, longFourteen,longEighteen,longTwenty_two,tomorrow_longTen;

    //倒计时
    public static long initCountDown() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, 1); //第2天，第x天，照加。如果是负数，表示前n天。

        //当前日期  字符串格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String nowTime=sdf.format(new Date());
        Zero =nowTime+" 00:00:00";
        Ten =nowTime+" 10:00:00";
        Twelve =nowTime+" 12:00:00";
        Fourteen =nowTime+" 14:00:00";
        Eighteen =nowTime+" 18:00:00";
        Twenty_two =nowTime+" 22:00:00";
        tomorrow = sdf.format(cal.getTime())+" 10:00:00";//第二天十点

        //当前日期  时间戳格式
        long  longZero = getStringToDate(Zero);
        long  longTen = getStringToDate(Ten);
        long  longTwelve = getStringToDate(Twelve);
        long  longFourteen = getStringToDate(Fourteen);
        long  longEighteen = getStringToDate(Eighteen);
        long  longTwenty_two = getStringToDate(Twenty_two);
        long  tomorrow_longTen = getStringToDate(tomorrow);

        //当前时间戳
        long  currentTime = System.currentTimeMillis();

        //当前时间  小时
        cal.setTimeInMillis(currentTime);
        int mHour=cal.get(Calendar.HOUR_OF_DAY); //24小时制 HOUR_OF_DAY

        //0-10点
        if (mHour>=0&&mHour<10){
            return Math.abs(longTen-currentTime);
        }//10-12点
        else if (mHour>=10&&mHour<12){
            return Math.abs(longTwelve-currentTime);
        }//12-14点
        else if (mHour>=12&&mHour<14){
            return Math.abs(longFourteen-currentTime);
        }//14-18点
        else if (mHour>=14&&mHour<18){
            return Math.abs(longEighteen-currentTime);
        }//18-22点
        else if (mHour>=18&&mHour<22){
            return Math.abs(longTwenty_two-currentTime);
        }//22点-第二天10点
        else if (mHour>=22&&mHour<24){
            return Math.abs(tomorrow_longTen-currentTime);
        }
        return Math.abs(longTen-currentTime);

    }


    /**
     * 将字符串转为时间戳
     * @param dateString
     * @param
     * @return
     */
    public static long getStringToDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try{
            date = dateFormat.parse(dateString);
        } catch(ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

}
